package cn.gson.crm.controller.money;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.crm.model.dao.PaybackDao;
import cn.gson.crm.model.entity.Payback;
import cn.gson.crm.model.entity.Payrecord;
import cn.gson.crm.model.mapper.PaybackMapper;

@Controller
@RequestMapping("/payback")
public class PayBackController {
	
	@Autowired
	private PaybackMapper paybackMapper;
	
	@Autowired
	private PaybackDao paybackDao;
	
	/**
	 * 查询所有的收款记录
	 * @param payrecord
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> index(Payback payback, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize){
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<Payback> pageData = paybackMapper.findAllPayback(payback);
		 
		PageInfo<Payback> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	
	/**
	 * 删除退款记录
	 * @param id
	 * @return
	 */
	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String,Object> delete(@PathVariable("id") Long id) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		Payback p;
		if (id != null && (p = paybackDao.findOne(id)) != null) {
			p.setDeleted(false);
			obj.put("success", true);
		} else {
			obj.put("message", "收款记录不存在！");
		}
		return obj;
	}
	/**
	 * 编辑表单
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping({ "add_payback", "add_payback/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		if (id != null) {
			Payback p = paybackDao.findOne(id);	
			if (p != null) {		
				model.addAttribute("payback", p);
			}
		}	
		List<Map<String,Object>> name=paybackMapper.selectAllStudentName();
		model.addAttribute("backName",name);
		return "payback/add_payback";
	}

	/**
	 * 保存
	 * @param stuinfor
	 * @param br
	 * @return
	 */
	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String,Object> save(@Valid Payback payback, BindingResult br) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("erros", br.getFieldErrors());
		} else {
			if (payback.getStudent().getId() == null) {
					obj.put("message", "该用户不存在！");
					return obj;
				
			} else {
				payback.setDeleted(true);
				payback.getStudent().setState("离校");
				paybackDao.save(payback);
				obj.put("success", true);
			}	
		}
		return obj;
	}
}
